import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:xim/common/model/file_type.dart';
import 'package:xim/common/module/qr.dart';
import 'package:xim/http/apis/gfs/gfs_api.dart';
import 'package:xim/http/apis/user/user_model.dart';
import 'package:xim/screen/login_screen/face_login_page.dart';
import 'package:xim/screen/login_screen/fingerprint_login_page.dart';
import 'package:xim/screen/main_screen.dart';
import 'package:xim/screen/message_list_screen/page/add_friend_page.dart';
import 'package:xim/screen/message_list_screen/page/add_friend_search_page.dart';
import 'package:xim/screen/message_list_screen/page/chat_detail_page/chat_detail_page.dart';
import 'package:xim/screen/message_list_screen/page/message_detail_info_page/chat_message_detail.dart';
import 'package:xim/screen/message_list_screen/page/chat_room_info_page.dart';
import 'package:xim/screen/message_list_screen/page/session_secretkey_detail_page.dart';
import 'package:xim/screen/page/file_view_page.dart';
import 'package:xim/screen/page/pdf_view_page.dart';
import 'package:xim/screen/page/photo_view_page.dart';
import 'package:xim/screen/page/qr_page.dart';
import 'package:xim/screen/page/qr_scan_page.dart';
import 'package:xim/screen/page/video_view_page.dart';
import 'package:xim/screen/page/web_view_page.dart';
import 'package:xim/screen/profile_screen/page/control_page.dart';
import 'package:xim/screen/profile_screen/page/detail_profile_info_page.dart';
import 'package:xim/screen/profile_screen/page/login_setting.dart';
import 'package:xim/screen/profile_screen/page/profile_public_page.dart';
import 'package:xim/screen/profile_screen/page/profile_self_info_page.dart';
import 'package:xim/widget/dash_chat_2/dash_chat_2.dart';

import '../screen/login_screen/login_page/login_page.dart';

final GoRouter router = GoRouter(
  initialLocation: '/login',
  routes: <RouteBase>[
    GoRoute(
      name: 'home',
      path: '/home',
      builder: (context, state) => const MainScreen(),
    ),
    GoRoute(
      name: 'login',
      path: '/login',
      builder: (context, state) => LoginScreen(),
      routes: [
        // GoRoute(
        //   path: 'pwd',
        //   builder: (context, state) => UserLoginPage(),
        // ),
        GoRoute(
          path: 'fingerprint',
          builder: (context, state){
            var user = state.extra as UserInfoModel;
            return FingerprintLoginPage(user: user,);
          },
        ),
        GoRoute(
          path: 'face',
          builder: (context, state){
            var user = state.extra as UserInfoModel;
            return FaceLoginPage(user: user,);
          },
        ),
      ]
    ),
    GoRoute(
      path: "/chat/session/:id",
      pageBuilder:(context, state){
        var id = state.pathParameters['id'];
        return CustomTransitionPage(
            key: state.pageKey,
            child: ChatDetailPage(sessionId: int.parse(id!),),
            transitionsBuilder: (context,animation, secondaryAnimation,child) {
              const begin = Offset(1, 0);
              const end = Offset.zero;
              const curve = Curves.ease;
              var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
              return SlideTransition(position:animation.drive(tween), child: child);
            }
        );
      },
      builder: (context, state){
        var id = state.pathParameters['id'];
        if(id!=null){
          return ChatDetailPage(sessionId: int.parse(id),);
        }
        // TODO 404 page
        return const Center(child: Text("404"),);
      }
    ),
    GoRoute(
      path: '/session/info/:id',
      builder: (context, state){
        var id = state.pathParameters['id'];
        if(id!=null){
          return ChatRoomInfoPage(sessionId: int.parse(id),);
        }
        // TODO 404 page
        return const Center(child: Text("404"),);
      }
    ),
    GoRoute(
      path: "/friend/add",
      name: 'addFriend',
      builder: ((context, state)=>AddFriendPage()),
      routes: [
        GoRoute(
          path: "search",
          builder: (context,state)=> AddFriendSearchPage(),
        )
      ]
    ),
    GoRoute(
      path: "/session/secretKey/:id/detail",
      builder:  ((context, state){
        var id = int.parse(state.pathParameters['id']!);
        return SessionSecretKeyDetailPage(secretKeyId: id,);
      }),
    ),
    GoRoute(
      path: "/message/detail/:id",
      builder: (context, state){
        var id = state.pathParameters['id']!;
        return ChatMessageDetailPage(int.parse(id));
      }
    ),
    GoRoute(
      path: "/setting/login",
      builder: (context, state)=>const LoginSetting()
    ),
    GoRoute(
      path: "/setting/control",
      builder: (context, state)=>ControlPage()
    ),

    GoRoute(
      path: "/profile",
      builder: (x,s)=>const Error404Page(),
      routes: [
        // GoRoute(
        //   path: "public/:account",
        //   builder: (context, state){
        //     final account = state.pathParameters['account']!;
        //     return ProfilePublicPage(account: account,);
        //   },
        //   routes: [
        //     GoRoute(
        //       path: "detail",
        //       builder: (context, state){
        //         final account = state.pathParameters['account']!;
        //         return DetailProfileInfoPage(account: account,);
        //       },
        //     ),
        //     GoRoute(
        //       path: "self",
        //       builder: (context, state){
        //         final account = state.pathParameters['account']!;
        //         return ProfileSelfInfoPage(account: account,);
        //       }
        //     )
        //   ]
        // ),
      ]
    ),
    GoRoute(
      path: "/qr",
      builder: (context, state)=>const Error404Page(),
      routes: [
        GoRoute(
            path: "user",
            builder: (context, state){
              var qrData = state.extra as IQrResult;
              return UserQrPage(qrData: qrData,);
            }
        ),
        GoRoute(
            path: "scan",
            builder: (context, state)=>const QrScanPage(),
        )
      ]
    ),
    GoRoute(
      path: "/web",
      builder: (context, state){
        final content = state.extra as String;
        return WebViewPage(content: content,);
      },
    ),
    GoRoute(
      path: "/public/view",
      builder: (context, state){
        var param = state.extra as MediaView;
        switch(param.fileType){
          case FileType.image:
            return PhotoViewPage(mediaView: param,);
          // case FileType.video:
          //   return VideoViewPage(mediaView: param,);
          case FileType.pdf:
            return PdfViewPage(mediaView: param,);
          default:
            return FileViewPage(mediaView: param,);
        }
      }
    )
  ],
);



class Error404Page extends StatelessWidget {
  const Error404Page({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: const Center(child: Text("404"),),
    );
  }
}


class MediaView {
  final String gfsName;
  final FileType fileType;
  final String plaintextAESKey;

  MediaView(this.gfsName, this.fileType, this.plaintextAESKey);

  MediaView.of(String gfsFileName, this.plaintextAESKey):
  gfsName = gfsFileName,
  fileType = FileType.parse(gfsFileName)
  ;

  String get gfsDownloadPath => GfsApi.getGfsFileDownloadPath(gfsName);

  String get fileExtension => gfsName.substring(gfsName.lastIndexOf(".")+1);
}
